package cn.mazexal.examapi.controller;

import cn.mazexal.examapi.models.bo.Credentials;
import cn.mazexal.examapi.models.enums.RequestHeaderEnum;
import cn.mazexal.examapi.utils.JWTProvider;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author: Mazexal
 * Date: 2025/6/4
 */
public class BaseController {

    @Resource
    private HttpServletRequest request;

    @Resource
    private JWTProvider jwtProvider;

    public Long getUserId() {
        String token = request.getHeader(RequestHeaderEnum.USER_TOKEN.getValue());
        Credentials credentials = jwtProvider.decode(token);
        return credentials.getUserId();
    }

    public String getVersionStr() {
        return request.getHeader(RequestHeaderEnum.VERSION.getValue());
    }

    public String getChannel() {
        return request.getHeader(RequestHeaderEnum.CHANNEL.getValue());
    }

    public Long getChannelId() {
        String channelIdStr = request.getHeader(RequestHeaderEnum.CHANNEL_ID.getValue());
        return StringUtils.isNotBlank(channelIdStr) ? Long.parseLong(channelIdStr) : 0L;
    }

    public Integer getVersion() {
        String version = getVersionStr();
        if (StringUtils.isNotBlank(version)) {
            version = version.replaceAll("\\.", "");
            return Integer.parseInt(version);
        } else {
            return 0;
        }
    }

    public String getIp() {
        String ipAddress = request.getHeader("x-forwarded-for");
        // K8S 中使用 x-original-forwarded-for
        if (StringUtils.isBlank(ipAddress)) {
            ipAddress = request.getHeader("x-original-forwarded-for");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
            ipAddress = request.getRemoteAddr();
            if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                // 根据网卡取本机配置的IP
                InetAddress address = null;
                try {
                    address = InetAddress.getLocalHost();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                ipAddress = address.getHostAddress();
            }
        }
        // 对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
        if (ipAddress != null && ipAddress.length() > 15) {
            // "***.***.***.***".length() = 15
            if (ipAddress.indexOf(",") > 0) {
                ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
            }
        }
        return ipAddress;
    }
}